//
//  DZAddress.m
//  DaoZiJinFu
//
//  Created by dzjf on 2016/11/4.
//  Copyright © 2016年 dzjf. All rights reserved.
//

#import "DZAddress.h"
#import "DZAddressCell.h"

@interface DZAddress ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *totalArray;
    NSMutableArray *provinceArray;
    NSMutableArray *cityArray;
    NSInteger flag;
    UITableView *_tableView;
    DZProvincesModel *firstModel;
}
@property (copy, nonatomic) NSString *cityStr;
@property (copy, nonatomic) NSString *provinceStr;
@property (strong, nonatomic) UIView *toolPickerView;

@end

@implementation DZAddress

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setView];
    }
    return self;
}

- (void)setView {
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    provinceArray = [NSMutableArray array];
    cityArray = [NSMutableArray array];
    [self getProvinceInformationWithID:@"0"];
    [self setUpPickerView];
}

- (void)getProvinceInformationWithID:(NSString *)pid {
    DZgetBankProvinceRequest *request = [[DZgetBankProvinceRequest alloc] init];
    request.p_id = pid;
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        if ([response[@"rt"] integerValue] == 1) {
            if (flag) {
                cityArray = [DZProvincesModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            }else{
                provinceArray = [DZProvincesModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            }
        }
        [_tableView reloadData];
    } failure:^(NSError *error) {
    }];
}

- (void)setUpPickerView {
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeCustom];
    cancle.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height - 250);
    [cancle addTarget:self action:@selector(toolBarDoneClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancle];
    
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-250, SCREEN_WIDTH, 250)];
    toolView.backgroundColor = [UIColor whiteColor];
    [self addSubview:toolView];
    _toolPickerView = toolView;
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 60, 20)];
    [cancelBtn setTitle:@"后退" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:ORANGECOLOR forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(toolBarBackClick) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:cancelBtn];
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-65, 10, 60, 20)];
    [confirmBtn setTitle:@"返回" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:ORANGECOLOR forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(toolBarDoneClick) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:confirmBtn];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
    line.backgroundColor = RGB(230, 230, 230);
    [toolView addSubview:line];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 210) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [toolView addSubview:_tableView];
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [_tableView registerNib:[UINib nibWithNibName:DZAddressCells bundle:nil] forCellReuseIdentifier:DZAddressCells];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (flag) {
        return cityArray.count;
    }else{
        return provinceArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DZAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:DZAddressCells];
    if (!flag) {
        DZProvincesModel *model = provinceArray[indexPath.row];
        cell.textLabel.text = model.name;
    }else{
        if (cityArray.count > indexPath.row) {
            DZProvincesModel *model = cityArray[indexPath.row];
            cell.textLabel.text = model.name;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (flag) {
        if (cityArray.count > indexPath.row) {
            DZProvincesModel *model = cityArray[indexPath.row];
            if (self.delegate &&[self.delegate respondsToSelector:@selector(DZAddressView:selectProvince:ProvinceId:city:cityId:)]) {
                [self.delegate DZAddressView:self selectProvince:firstModel.name ProvinceId:firstModel.code city:model.name cityId:model.code];
            }
        }
        return;
    }
    if (provinceArray.count > indexPath.row) {
        flag = 1;
        DZProvincesModel *model = provinceArray[indexPath.row];
        firstModel = model;
        [self getProvinceInformationWithID:[NSString stringWithFormat:@"%ld",model.id]];
    }
}


- (void)toolBarBackClick {
    flag = 0;
    [_tableView reloadData];
}

- (void)toolBarDoneClick {
    [self removeFromSuperview];
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end


@implementation DZProvincesModel
@end

